#include "newgame.hpp"

#include <QStringList>

NewGame::NewGame(Client * client, QWidget * parent) :
    QWidget(parent),
    _client(client)
{
    _layout = new QGridLayout(this);
    setLayout(_layout);

    _labelGoban = new QLabel("Goban size", this);
    _labelKomi = new QLabel("Komi", this);

    _goban = new QComboBox(this);
    _komi = new QDoubleSpinBox(this);

    _create = new QPushButton("Create", this);
    _cancel = new QPushButton("Cancel", this);

    _komi->setRange(0, 10);
    _komi->setDecimals(1);
    _komi->setSingleStep(0.5);
    _komi->setValue(6.5);

    QStringList cbv;
    cbv.append("19x19");
    cbv.append("13x13");
    cbv.append("9x9");

    _goban->insertItems(0, cbv);

    _layout->addWidget(_labelGoban, 0, 0);
    _layout->addWidget(_goban, 0, 1);
    _layout->addWidget(_labelKomi, 1, 0);
    _layout->addWidget(_komi, 1, 1);
    _layout->addWidget(_create, 2, 0);
    _layout->addWidget(_cancel, 2, 1);

    connect(_create, SIGNAL(clicked()), this, SLOT(reactCreateClicked()));
    connect(_cancel, SIGNAL(clicked()), this, SLOT(close()));
}

void NewGame::reactCreateClicked()
{
    int index = _goban->currentIndex();
    int size = 0;

    if (index == 0)
        size = 19;
    else if (index == 1)
        size = 13;
    else
        size = 9;

    _client->send(Mid::CREATEGAME, size, _komi->value());
    close();
}
